/**
 * 
 */
package nguyen.hoan.sharelocation.service;

import java.util.ArrayList;

import nguyen.hoan.sharelocation.model.FriendSearchModel;
import nguyen.hoan.sharelocation.model.GMapV2Direction;

import org.w3c.dom.Document;

import android.content.Context;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

/**
 * @author Hoan Apr 5, 2014 3:16:52 PM
 * 
 */
public class MyGoogleMapService implements LocationListener {

	private static final long MIN_TIME = 1000;
	private static final float MIN_DISTANCE = 1000;
	private static final String SHARE_LOCATION_MSG = "Share this location...";
	private static final String FRIEND_LOCATION_MSG = "Share your friend's location...";
	private static final String SHARE_YOUR_LOCATION_MSG = "Share your detected location...";

	private Location myLocation = null;
	private Context context;
	private GoogleMap googleMap;
	private LocationManager locationManager;

	public MyGoogleMapService(Context context, GoogleMap googleMap) {

		this.context = context;
		this.googleMap = googleMap;
		this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
	}

	public void settingMapUI() {

		if (googleMap != null) {
			googleMap.getUiSettings().setMyLocationButtonEnabled(true);
			googleMap.getUiSettings().setZoomControlsEnabled(true);
			googleMap.getUiSettings().setCompassEnabled(true);
			googleMap.getUiSettings().setRotateGesturesEnabled(true);
			googleMap.getUiSettings().setTiltGesturesEnabled(true);
			googleMap.getUiSettings().setAllGesturesEnabled(true);
			googleMap.getUiSettings().setZoomGesturesEnabled(true);
			googleMap.getUiSettings().setScrollGesturesEnabled(true);

			googleMap.setMyLocationEnabled(true);
		}

	}

	public void bindMapActionListener() {

		googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

			@Override
			public void onInfoWindowClick(Marker marker) {
				MyShareLocationService.getInstance().shareLocationMessage(context, marker.getPosition());
			}
		});

		googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

			@Override
			public void onMapLongClick(LatLng latLng) {
				googleMap.clear();
				drawMyLocationMarker(SHARE_YOUR_LOCATION_MSG);
				drawMarker(latLng, SHARE_LOCATION_MSG);
			}
		});

		googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

			@Override
			public void onMapClick(LatLng latLng) {
				googleMap.clear();
				drawMyLocationMarker(SHARE_YOUR_LOCATION_MSG);
			}
		});
	}

	public void bindLocationChangedListener() {

		if (locationManager != null) {
			locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
			locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DISTANCE, this);
		}
	}

	public void searchAndShowFriendLocation(FriendSearchModel model) {

		// drawMarker(new
		// LatLng(Double.valueOf(model.getLatitude()).doubleValue(),
		// Double.valueOf(model.getLongitude()).doubleValue()),
		// FRIEND_LOCATION_MSG);

		// LatLng fromPosition = new LatLng(myLocation.getLatitude(),
		// myLocation.getLongitude());
		// LatLng toPosition = new
		// LatLng(Double.valueOf(model.getLatitude()).doubleValue(),
		// Double.valueOf(model.getLongitude()).doubleValue());

		new AsyncTask<Void, Void, Void>() {

			@Override
			protected Void doInBackground(Void... params) {
				LatLng fromPosition = new LatLng(13.687140112679154, 100.53525868803263);
				LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);

				GMapV2Direction md = new GMapV2Direction();

				Document doc = md.getDocument(fromPosition, toPosition, GMapV2Direction.MODE_DRIVING);
				ArrayList<LatLng> directionPoint = md.getDirection(doc);
				PolylineOptions rectLine = new PolylineOptions().width(3).color(Color.RED);

				for (int i = 0; i < directionPoint.size(); i++) {
					rectLine.add(directionPoint.get(i));
				}

				googleMap.addPolyline(rectLine);

				return null;
			}
		}.execute();
	}

	/**
	 * @param mapType
	 *            GoogleMap.MAP_TYPE_NORMAL MAP_TYPE_NORMAL = 1;
	 *            MAP_TYPE_SATELLITE = 2; MAP_TYPE_HYBRID = 4;
	 */
	public void changeMapType(int mapType) {
		googleMap.setMapType(mapType);
	}

	@Override
	public void onLocationChanged(Location location) {
		this.myLocation = location;

		LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
		CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
		googleMap.animateCamera(cameraUpdate);
		googleMap.clear();
		drawMarker(new LatLng(location.getLatitude(), location.getLongitude()), SHARE_YOUR_LOCATION_MSG);

		locationManager.removeUpdates(this);
	}

	private void drawMarker(LatLng latLng, String infoMsg) {
		MarkerOptions marker = new MarkerOptions().position(latLng).title(infoMsg);
		googleMap.addMarker(marker).showInfoWindow();
	}

	private void drawMyLocationMarker(String infoMsg) {
		if (myLocation != null) {
			drawMarker(new LatLng(myLocation.getLatitude(), myLocation.getLongitude()), infoMsg);
		}
	}

	@Override
	public void onProviderDisabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onProviderEnabled(String arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
		// TODO Auto-generated method stub

	}

}
